package com.hui.security.sys.controller;

import com.hui.security.common.response.ResultMapper;
import com.hui.security.common.response.ResultVO;
import com.hui.security.sys.entity.DataRule;
import com.hui.security.sys.service.DataRuleService;
import com.hui.security.sys.vo.DataRuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b><code>DataRuleController</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 22:31.
 *
 * @author HuWeihui
 */
@RestController
@RequestMapping("/v1")
@Api(value = "数据规则API", tags = "data-rule")
@Slf4j
public class DataRuleController {

    /**
     * The Data rule service.
     */
    @Autowired
    private DataRuleService dataRuleService;

    /**
     * Create data rule result vo.
     *
     * @param dataRuleVO    the data rule vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "新增数据规则操作", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/data-rules")
    public ResultVO createDataRule(@RequestBody @Validated DataRuleVO dataRuleVO, BindingResult bindingResult) {
        DataRule dataRule = new DataRule();
        BeanUtils.copyProperties(dataRuleVO, dataRule);
        return ResultMapper.ok(dataRuleService.add(dataRule));
    }

    /**
     * Delete data rule result vo.
     *
     * @param ruleId the rule id
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "删除数据规则", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DeleteMapping("/data-rules/{id}")
    public ResultVO deleteDataRule(@PathVariable("id") String ruleId) {
        dataRuleService.delete(ruleId);
        return ResultMapper.ok();
    }

    /**
     * Update data rule result vo.
     *
     * @param dataRuleVO    the data rule vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "更新数据规则")
    @PutMapping("/data-rules")
    public ResultVO updateDataRule(@RequestBody @Validated DataRuleVO dataRuleVO, BindingResult bindingResult) {
        DataRule dataRule = new DataRule();
        BeanUtils.copyProperties(dataRuleVO, dataRule);
        dataRuleService.update(dataRule);
        return ResultMapper.ok();
    }

    /**
     * Gets data rule.
     *
     * @param ruleId the rule id
     * @return the data rule
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询数据规则详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/data-rules/{id}")
    public ResultVO getDataRule(@PathVariable("id") String ruleId) {
        DataRule dataRule = dataRuleService.get(ruleId);
        return ResultMapper.ok(dataRule);
    }

    /**
     * List all result vo.
     *
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询所有数据规则", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/data-rules")
    public ResultVO listDataRules(@RequestParam String appId) {
        return ResultMapper.ok();
    }
}
